package com.ruoyi.web.controller.evaluation;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.QueClassification;
import com.ruoyi.system.domain.SysRole;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.evaluation.Evaluation;
import com.ruoyi.system.domain.evaluation.Location;
import com.ruoyi.system.domain.evaluation.Store;
import com.ruoyi.system.domain.evaluation.StoreLocation;
import com.ruoyi.system.domain.waterEnity.ProductInfoBO;
import com.ruoyi.system.service.evaluationService.LocationService;
import com.ruoyi.system.service.evaluationService.StoreService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/evaluation")
public class StoreController extends BaseController {
    @Autowired
    private StoreService storeService;
    @Autowired
    private LocationService locationService;
    private String prefix = "evaluation/";

    /**
     * 返回商铺地址
     * @return
     */
    @RequiresPermissions("evaluation:store:view")
    @RequestMapping("/store")
    public String store(ModelMap map){
        List<Location> locations =locationService.selectLocation();
        map.put("locations",locations);
        return "evaluation/store";
    }

    /**
     * 返回商铺数据
     * @return
     */
    @RequiresPermissions("evaluation:store:view")
    @PostMapping("/listStore")
    @ResponseBody
    public TableDataInfo list(String location_id,String score,String storeName){
        startPage();
        System.out.println(location_id);
        System.out.println(score);
        List<StoreLocation> stores = storeService.selectStoreList(storeName,location_id,score);
        return getDataTable(stores);
    }
    /**
     * 导出商品评价数据
     * @param ids
     * @return
     */
    @RequiresPermissions("evaluation:store:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(String ids){
        System.out.println("ids================"+ids);
        //加入所选择的参数，可以导出全部或者选定的部分
//        List<StoreLocation> list = storeService.selectStoreLocation();
        List<StoreLocation> list = storeService.selectStoreByListId(ids);
        ExcelUtil<StoreLocation> util = new ExcelUtil<StoreLocation>(StoreLocation.class);
        return util.exportExcel(list,"商铺评价数据");
    }

    /**
     * 删除商铺评价数据
     * @return
     */
    @RequiresPermissions("evaluation:store:remove")
    @PostMapping("/deleteEvaluation")
    @ResponseBody
    public AjaxResult deleteEvaluation(){
        int num = storeService.updateScoreAndCount();
        if (num>0){
            return success();
        }else{
            return  error("将评价数据清空出错！！");
        }
    }

    /**
     * 通过location—_id查询地址
     * @param location_id
     * @return
     */
    @RequiresPermissions("evaluation:store:view")
    @GetMapping("/selectLocationByID")
    public List<Store> selectLocationByID(@RequestParam("location_id") int location_id){
        return storeService.selectStoreByLocation_id(location_id);
    }

    /**
     * 跳转到添加页面
     * @return
     */
    @RequiresPermissions("evaluation:store:view")
    @GetMapping("/addStore")
    public String addStore() {
    return prefix + "/addStore";
    }

    /**
     * 添加商铺
     * @param request
     * @param store
     * @return
     */
    @RequiresPermissions("evaluation:store:add")
    @PostMapping("/addStore1")
    @ResponseBody
    public AjaxResult addStore1(HttpServletRequest request, Store store){
        System.out.println(store);
        store.setCount(0);
        store.setScore(0);
       int num = storeService.insertStore(store);
       if (num>0){
           return success("添加商铺成功");
       }else{
           return error("添加商铺失败");
       }
    }

    /**
     * 跳转到编辑页面
     * @param store_id
     * @param map
     * @return
     */
    @RequiresPermissions("evaluation:store:edit")
    @RequestMapping("/editStore/{store_id}")
    public String editStore(@PathVariable("store_id") int store_id,ModelMap map){
        //根据ID查找商铺
        Store store = storeService.selectStoreByStore_id(store_id);
        List<Location> locations = locationService.selectLocation();
        map.put("store",store);
        map.put("locations",locations);
        return "evaluation/editStore";
    }
    /**
     * 编辑商铺
     * @param id
     * @return
     */
    @RequiresPermissions("evaluation:store:edit")
    @PostMapping("/editStore2/{id}")
    @ResponseBody
    public AjaxResult editSave(@PathVariable("id") int id,Store store)
    {
        System.out.println(id);
        System.out.println(store);
      //更新信息
        int num = storeService.updateStore(store);
        if (num>0){
            return success();
        }else {
            return error();
        }
    }
    /**
     * 删除商铺信息
     */
    @RequiresPermissions("evaluation:store:remove")
    @RequestMapping("/removeStore/{store_id}")
    public AjaxResult removeStore(@PathVariable("store_id") int store_id){
        //删除商铺
        int num = storeService.deleteStoreByID(store_id);
        if (num>0){
            return success();
        }else{
            return error();
        }

    }
    /**
     * 删除商铺信息
     */
    @RequiresPermissions("evaluation:store:remove")
    @RequestMapping("/removeStore2")
    @ResponseBody
    public AjaxResult removeStore2(String ids){
        //删除商铺
        int num = storeService.deleteStore(ids);
        if (num>0){
            return success();
        }else{
            return error();
        }

    }
}
